/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.ui.view;

import java.util.Collection;

import com.siro.pm.model.Project;
import com.siro.pm.model.provider.MenuTreeProvider;
import com.siro.pm.model.provider.ProjectProvider;
import com.siro.pm.ui.detail.ProjectDetailPage;
import com.siro.pm.ui.main.BugsPage;
import com.siro.pm.ui.main.DevelopersPage;
import com.siro.pm.ui.main.DocumentsPage;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Tree;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since ProjectView.java v1.0
 */
public class ProjectView extends HorizontalSplitPanel {
	private static final long serialVersionUID = 5805653924799037155L;

	private Project userSelectProject;
	Tree menuTree;

	public ProjectView() {

	}

	public void createContents() {
		setSizeFull();
		setSplitPosition(20, Sizeable.UNITS_PERCENTAGE);
		// Menu tree, initially shown
		CssLayout menuLayout = new CssLayout();
		menuLayout.addComponent(createMenuTree());
		setFirstComponent(menuLayout);

		// Expand select Project
		if (userSelectProject != null) {
			// menuTree.expandItem(userSelectProject);
			Collection<?> rootItemIds = menuTree.rootItemIds();
			for (Object object : rootItemIds) {
				Project temp = (Project) object;
				if (temp.getId().equals(userSelectProject.getId())) {
					menuTree.expandItem(temp);
					break;
				}
			}
			menuTree.select(userSelectProject);
		}
	}

	private Component createMenuTree() {
		menuTree = new Tree();
		menuTree.setImmediate(true);
		menuTree.setStyleName("menu");
		menuTree.setContainerDataSource(MenuTreeProvider.getTreeMenuContainer());
		menuTree.setItemCaptionPropertyId(MenuTreeProvider.PROJECT_PROPERTY_NAME);
		menuTree.setItemIconPropertyId(MenuTreeProvider.PROJECT_PROPERTY_ICON);
		menuTree.addListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1L;

			public void valueChange(ValueChangeEvent event) {
				Object value = event.getProperty().getValue();
				if (value != null) {
					Project project = (Project) menuTree
							.getItem(value)
							.getItemProperty(
									MenuTreeProvider.PROJECT_PROPERTY_PROJECT)
							.getValue();
					String item_mane = (String) menuTree
							.getItem(value)
							.getItemProperty(
									MenuTreeProvider.PROJECT_PROPERTY_NAME)
							.getValue();
					if (value instanceof String) {
						if (item_mane
								.equals(ProjectProvider.PROJECT_PROPERTY_DEVELOPERS)) {
							DevelopersPage developerPage = new DevelopersPage();
							developerPage.setProject(project);
//							developerPage.setDevelopers(project.getDevelopers());
							developerPage.createContents();
							setSecondComponent(developerPage);
						} else if (item_mane
								.equals(ProjectProvider.PROJECT_PROPERTY_DOCUMENTS)) {
							DocumentsPage documentPage = new DocumentsPage();
							documentPage.setProject(project);
//							documentPage.setDocuments(project.getDocs());
							documentPage.createContents();
							setSecondComponent(documentPage);
						} else if (item_mane
								.equals(ProjectProvider.PROJECT_PROPERTY_BUGS)) {
							BugsPage bugsPage = new BugsPage();
							bugsPage.setProject(project);
//							bugsPage.setBugs(project.getDemandsOrBugs());
							bugsPage.createContents();
							setSecondComponent(bugsPage);
						}
					} else if (value instanceof Project) {
						ProjectDetailPage projectPage = new ProjectDetailPage();
						projectPage.setProject(project);
						projectPage.createContents();
						setSecondComponent(projectPage);
					}
				}
			}
		});
		return menuTree;
	}

	public Project getUserSelectProject() {
		return userSelectProject;
	}

	public void setUserSelectProject(Project userSelectProject) {
		this.userSelectProject = userSelectProject;
	}

}
